package com.itheima.controller.advice;

import com.itheima.controller.BusinessException;
import com.itheima.po.Code;
import com.itheima.po.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author Andy
 * @Description //TODO GlobalExceptionHandler
 * @Date 2022/3/2 9:00 上午
 * @Version 1.0
 **/


//@ControllerAdvice // 作用类上，作用是开启controller类的aop处理，内部@Component会
////默认注册对象到spring的IOC容器里
//@ResponseBody // 返回数据本身
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result exception(Exception ex){
        System.out.println(ex);
        return new Result(Code.SYSTEM_ERROR, Code.SYSTEM_ERROR_MSG, null);
    }

    @ExceptionHandler(BusinessException.class)
    public Result exception(BusinessException ex){
        System.out.println(ex);
        return new Result(ex.getCode(), ex.getMessage(), null);
    }
}
